<?php
class ResumesController extends AppController {

	var $name = 'Resumes';
	var $helpers = array('Html', 'Form');
    
    function preview($id = null)       
    {
        if( $this->Session->read('User.type')=='jobseeker' || $this->Session->read('User.type')=='employer')
        {
            $data = $this->Resume->read( null,$id );
            if($this->Session->read('User.type')=='jobseeker')
            {
                if($data['Resume']['jobseeker_id']==$this->Session->read('User.id') )
                {}                
                else
                {
                    $this->Session->setFlash(__('This is not your\'s', true));
                    $this->redirect(array('controller'=>'resumes', 'action'=>'index'),null, true);        
                }
            }
            else
            {
                $temp['Resume']['id'] = $id;
                $temp['Resume']['views'] = $data['Resume']['views']+1;
                $this->Resume->save($temp);
            }
            $this->set('data',$data);
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);        
        } 
    }
    
	function summary()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->set('choose1',32);
            $this->set('menu_resume',3);
            if(!empty($this->data))
            {
                if($this->data['Resume']['avaibility']!='1')
                { $this->data['Resume']['date_avaible']=null; }
                if($this->data['Resume']['work_exp']!='2')
                { 
                    $this->data['Resume']['month_exp']=0; 
                    $this->data['Resume']['year_exp']=0; 
                }
                $this->Resume->save($this->data);
                $this->redirect(array('controller'=>'resumes', 'action'=>'searchable'));
            }
            if (empty($this->data))
            {
                $this->Resume->recursive = -1;
                $this->data = $this->Resume->findByJobseekerId($this->Session->read('User.id'));
            }
            $academics = $this->Resume->Academic->find('list'); 
            $this->set('academics',$academics);   

        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
        }        
    }
    
    function reset_views()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->data = $this->Resume->findByJobseekerId($this->Session->read('User.id'));
            $this->data['Resume']['views'] = 0;
            $this->Resume->save($this->data);
            
            $this->Session->setFlash(__('Your resume has been reset', true));
            $this->redirect(array('controller'=>'resumes', 'action'=>'searchable'),null, true);

        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
        }        
    
    }      
    
    function searchable()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->set('choose1',32);
            $this->set('menu_resume',4);            
            if(!empty($this->data))
            {
                $this->Resume->save($this->data);
                $this->redirect(array('controller'=>'resumes', 'action'=>'attachment'),null, true);
            }
            if (empty($this->data))
            {
                $this->Resume->recursive = -1;
                $this->data = $this->Resume->findByJobseekerId($this->Session->read('User.id'));
            }
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
        }        
    
    }      
    
    
    function attachment()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->set('choose1',32);
            $this->set('menu_resume',5);
            if(!empty($this->data))
            {
                if($this->data['Resume']['upload_file']['name']!='')
                {
                    if( $this->data['Resume']['upload_file']['type']=='application/pdf' || $this->data['Resume']['upload_file']['type']=='application/msword' || $this->data['Resume']['upload_file']['type']=='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
                    {

                        $name = substr($this->data['Resume']['upload_file']['name'],0,strpos($this->data['Resume']['upload_file']['name'],'.'));
                        $type = substr($this->data['Resume']['upload_file']['name'],strpos($this->data['Resume']['upload_file']['name'],'.'),5); 
                        $destination = RESUME_DIRECTORY.$this->data['Resume']['id'].$type;

                        $this->Resume->recursive = -1;
                        $data = $this->Resume->findByJobseekerId($this->Session->read('User.id'));
                        
                        if( file_exists($data['Resume']['upload_file']) && $data['Resume']['upload_file']!='' && $data['Resume']['upload_file']!='n/a' )
                        {
                            unlink($data['Resume']['upload_file']);
                        }
                        
                        if( move_uploaded_file($this->data['Resume']['upload_file']['tmp_name'],$destination)  )
                        {

                            $this->Session->setFlash(__('Your resume attachment is uploaded.', true));
                            $this->data['Resume']['upload_file']=$destination;
                            $this->Resume->save($this->data); 
                            $this->redirect(array('controller'=>'resumes', 'action'=>'photo'),null, true);
                        }
                        else
                        { $this->Session->setFlash(__('File is failed to upload, please try again', true)); }

                    }
                    else
                    { $this->Session->setFlash(__('Please provide file with .pdf / .doc / .docx format', true)); }                    
                }
                else
                { $this->Session->setFlash(__('Please provide file with .pdf / .doc / .docx format', true)); }                    

            }
            $this->Resume->recursive = -1;
            $this->data = $this->Resume->findByJobseekerId($this->Session->read('User.id'));
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
        }    
    }
    
    function delete_attachment()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
                $this->Resume->recursive = -1;
                $this->data = $this->Resume->findByJobseekerId($this->Session->read('User.id'));
                
                if( file_exists($this->data['Resume']['upload_file']) && $this->data['Resume']['upload_file']!='' && $this->data['Resume']['upload_file']!='n/a' )
                {
                    unlink($this->data['Resume']['upload_file']);
                    $this->Session->setFlash(__('Your resume attachment has been deleted.', true));
                }
                else
                {
                    $this->Session->setFlash(__('Failed to delete the file.', true));
                }
                
                $this->data['Resume']['upload_file']='';
                $this->Resume->save($this->data); 
                $this->redirect(array('controller'=>'resumes','action'=>'attachment'));           
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);        
        }
    } 


    function photo()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->set('choose1',32);
            $this->set('menu_resume',6);
            if(!empty($this->data))
            {
                if($this->data['Resume']['upload_photo']['name']!='')
                {
                    if($this->data['Resume']['upload_photo']['type']=='image/gif' || $this->data['Resume']['upload_photo']['type']=='image/jpeg' || $this->data['Resume']['upload_photo']['type']=='image/pjpeg' || $this->data['Resume']['upload_photo']['type']=='image/png')
                    {
                        $size = getimagesize($this->data['Resume']['upload_photo']['tmp_name']);
                        if($size[0]<=110 && $size[1]<=150)
                        {

                            $this->Resume->recursive = -1;
                            $data = $this->Resume->findByJobseekerId($this->Session->read('User.id'));
                            
                            if( file_exists($data['Resume']['upload_photo']) && $data['Resume']['upload_photo']!='' && $data['Resume']['upload_photo']!='n/a' )
                            {
                                unlink($data['Resume']['upload_photo']);
                            }


                            $name = substr($this->data['Resume']['upload_photo']['name'],0,strpos($this->data['Resume']['upload_photo']['name'],'.'));
                            $type = substr($this->data['Resume']['upload_photo']['name'],strpos($this->data['Resume']['upload_photo']['name'],'.'),5); 
                            $destination = PHOTO_DIRECTORY.$this->data['Resume']['id'].$type;
                            if( move_uploaded_file($this->data['Resume']['upload_photo']['tmp_name'],$destination)  )
                            {

                                $this->Session->setFlash(__('Your photo has been uploaded.', true));
                                $this->data['Resume']['upload_photo']=$destination;
                                $this->Resume->save($this->data); 
 //                               $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);

                            }
                            else
                            { $this->Session->setFlash(__('The file is failed to be uploaded, please try again.', true));}
                        }
                        else
                        { $this->Session->setFlash(__('Maximum size is 110 x 150 pixel', true)); }
                    }
                    else
                    { $this->Session->setFlash(__('Please provide image file (.jpeg, .jpg, .gif, .png)', true)); }                    
                }
                else
                { $this->Session->setFlash(__('Please provide image file (.jpeg, .jpg, .gif, .png)', true)); }                    

            }
            $this->Resume->recursive = -1;
            $this->data = $this->Resume->findByJobseekerId($this->Session->read('User.id'));
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true); 
        }        
    }   
  
    function delete_photo()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
                $this->Resume->recursive = -1;
                $this->data = $this->Resume->findByJobseekerId($this->Session->read('User.id'));
                
                if( file_exists($this->data['Resume']['upload_photo']) && $this->data['Resume']['upload_photo']!='' && $this->data['Resume']['upload_photo']!='n/a' )
                {
                    unlink($this->data['Resume']['upload_photo']);
                    $this->Session->setFlash(__('Your photo has been delete.', true));
                }
                else
                {
                    $this->Session->setFlash(__('You don\'t have uploaded photo', true));
                }
                
                $this->data['Resume']['upload_photo']='';
                $this->Resume->save($this->data); 
                $this->redirect(array('controller'=>'resumes','action'=>'photo'));           
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true); 
        }  
    } 
 
 
    function additional()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->set('choose1',32);
            $this->set('menu_resume',11);
            if(!empty($this->data))
            {
                $this->data['Resume']['self_desc'] = strip_tags($this->data['Resume']['self_desc'],'<br>');
                $this->data['Resume']['certificate'] = strip_tags($this->data['Resume']['certificate'],'<br>');
                
                $this->Resume->save($this->data);
                $this->redirect(array('controller'=>'jobseekers', 'action'=>'view_resume'),null, true);
            }
            if (empty($this->data))
            {
                $this->Resume->recursive = -1;
                $this->data = $this->Resume->findByJobseekerId($this->Session->read('User.id'));
            }
            
        }
        else
        { 
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
        }      
    }    
    
    function search()
    {
        if($this->Session->read('User.type')=='employer')
        {
            $this->set('choose1',45);
            $academics = $this->Resume->query("SELECT * FROM `academics`");
            $cities = $this->Resume->query("SELECT * FROM `cities` ORDER BY cities.name");
            $this->set('academics',$academics);
            $this->set('cities',$cities);
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true);        
        }
    }
    
    function search_result()
    {
        if($this->Session->read('User.type')=='employer')
        {
            if(empty($this->data))
            {
                $this->Session->setFlash(__('Please start a new search', true));
                $this->redirect(array('controller'=>'resumes', 'action'=>'search'),null, true);                
            }
            $this->set('choose1',45);    
            if($this->data['Resume']['use_skill']==1)
            { $query = "SELECT distinct(resumes.id) FROM `resumes`,`cities`,`jobseekers`,`academics`,`skills` WHERE jobseekers.id = resumes.jobseeker_id AND jobseekers.city_id = cities.id AND skills.jobseeker_id = jobseekers.id AND resumes.academic_id = academics.id"; }
            else
            { $query = "SELECT resumes.id FROM `resumes`,`cities`,`jobseekers`,`academics` WHERE jobseekers.id = resumes.jobseeker_id AND jobseekers.city_id = cities.id AND resumes.academic_id = academics.id"; }
            
            
            $query.= " AND resumes.title LIKE '%".$this->data['Resume']['title']."%'";
            if($this->data['Resume']['city']!=0)
            { $query.= " AND cities.id =".$this->data['Resume']['city']; }
            if($this->data['Resume']['academic']!=0)
            { $query.= " AND academic.id >=".$this->data['Resume']['academic']; }
            
            if($this->data['Resume']['exp']==1)
            { $query.= " AND resumes.work_exp = 1"; }
            if($this->data['Resume']['exp']==2)
            { $query.= " AND resumes.work_exp = 2"; }
            if($this->data['Resume']['exp']==3)
            { $query.= " AND resumes.year_exp = 1"; }
            if($this->data['Resume']['exp']==4)
            { $query.= " AND resumes.year_exp = 2"; }
            if($this->data['Resume']['exp']==5)
            { $query.= " AND resumes.year_exp = 3"; }
            if($this->data['Resume']['exp']==6)
            { $query.= " AND resumes.year_exp > 3"; }
        
            if($this->data['Resume']['use_skill']==1)
            {
                $skills = explode(",",$this->data['Resume']['skill']);
                
                if($this->data['Resume']['opskill']==1)
                { $word = ' OR '; }
                else
                { $word = ' AND '; }
                    
                    $i=0;
                    $query .= " AND ( ";
                    foreach($skills as $skill)
                    {
                        if($i)
                        { $query .= $word; }
                        $query .= " skills.name LIKE '%".$skill."%'";
                        $i++;
                    }
                    $query .= " ) ";
            }
            
            if($this->data['Resume']['use_personal']==1)
            {
                if($this->data['Resume']['gender']==1)
                $query.= " AND jobseekers.gender = 'Male'";
                if($this->data['Resume']['gender']==2)
                $query.= " AND jobseekers.gender = 'Female'";
                
                if($this->data['Resume']['martial']==1)
                $query.= " AND jobseekers.martial_stats = 'Single'";
                
                $date1 = date("Y-m-d", mktime(0, 0, 0, date('m'), date('d'), date('Y')-$this->data['Resume']['age1']));
                $date2 = date("Y-m-d", mktime(0, 0, 0, date('m'), date('d'), date('Y')-$this->data['Resume']['age2']));
                
                $query.= " AND ( jobseekers.date_of_birth >= '".$date2."' AND jobseekers.date_of_birth <= '".$date1."' ) ";
            }
            
            if($this->data['Resume']['use_misc']==1)
            {
                if($this->data['Resume']['relocate']==1)
                { $query.= " AND ( resumes.relocate='will consider' OR resumes.relocate='yes' )"; }
                if($this->data['Resume']['overseas']==1)
                { $query.= " AND ( resumes.overseas='will consider' OR resumes.overseas='yes' )"; }
                if($this->data['Resume']['vehicle']==1)
                { $query.= " AND resumes.vehicle <> 'none' "; }
            }
            
            $query2 = " SELECT resumes.*,jobseekers.* FROM `resumes`,`jobseekers` WHERE jobseekers.id = resumes.jobseeker_id AND resumes.open=1 AND resumes.id IN (".$query.") ";
            $data = $this->Resume->query($query2);
            $this->set('data',$data);
            $cities = $this->Resume->query("SELECT cities.* FROM `cities` ORDER BY cities.name");
            $academics = $this->Resume->query("SELECT academics.* FROM `academics` ORDER BY academics.id");
            $this->set('cities',$cities);
            $this->set('academics',$academics);
        }
    
    }
 
    function get_data()
    {
        $this->Resumes->recursive=-1;
        $data = $this->Resume->find('first',array('conditions'=>array('Resume.jobseeker_id'=>$this->Session->read('User.id')) ));
        return $data; 
    }

    
    function index() 
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->set('choose1',32);
            $this->set('menu_resume',1);
            
            $data = $this->Resume->read( null,$this->Session->read('User.id') );
            $this->set('data',$data);
        }
        else
        {
            $this->set('choose1',3);
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);        
        }
	}

}
?>